﻿using AspNetCoreWebMiddleware.Middleware;
using AspNetCoreWebMiddleware.Model;
using System.IO.Pipelines;
using System.Text;

namespace AspNetCoreWebMiddleware
{
    internal class Program
    {
        public static async Task ResposeHtml(HttpContext httpContext)
        {
            // 1、设置内容类型
            httpContext.Response.ContentType = "text/html";
            // 2、获取html文件路径
            var filepath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "index.html");
            // 3、读取文件内容
            var htmlContent = await File.ReadAllTextAsync(filepath);
            // 4、响应输出
            await httpContext.Response.WriteAsync(htmlContent);
        }

        public static async Task ResposeCss(HttpContext httpContext)
        {
            // 设置响应内容类型为 css
            httpContext.Response.ContentType = "text/css";

            // 获取 JavaScript 文件的路径
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "index.css");

            // 读取 JavaScript 文件内容
            var jsContent = await File.ReadAllTextAsync(filePath);

            // 响应 JavaScript 文件内容
            await httpContext.Response.WriteAsync(jsContent);
        }

        public static async Task ResposeJs(HttpContext httpContext)
        {
            // 设置响应内容类型为
            httpContext.Response.ContentType = "application/javascript";

            // 获取 JavaScript 文件的路径
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "index.js");

            // 读取 JavaScript 文件内容
            var jsContent = await File.ReadAllTextAsync(filePath);

            // 响应 JavaScript 文件内容
            await httpContext.Response.WriteAsync(jsContent);
        }

        static void Main(string[] args)
        {
            #region web主机运行项目
            {
                ////WebApplicationBuilder
                //WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder();
                ////创建泛型主机
                ////webApplicationBuilder.Services.AddControllers();
                //webApplicationBuilder.Services.AddMvc();
                //WebApplication webApplication = webApplicationBuilder.Build();
                ////运行web
                //webApplication.Run();
            }
            #endregion

            #region web主机使用 - 端口监听
            {
                ////WebApplicationBuilder
                //WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder();
                ////创建泛型主机
                //webApplicationBuilder.Services.AddControllers();
                //webApplicationBuilder.Services.AddMvc();
                //WebApplication webApplication = webApplicationBuilder.Build();
                ////运行web
                //webApplication.Run("http://localhost:5008");
            }
            #endregion

            #region web主机使用 - 多端口监听
            {
                ////WebApplicationBuilder
                //WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder();
                ////创建泛型主机
                //webApplicationBuilder.Services.AddControllers();
                //webApplicationBuilder.Services.AddMvc();
                //WebApplication webApplication = webApplicationBuilder.Build();
                ////运行web
                //webApplication.Urls.Add("http://localhost:5008");
                //webApplication.Urls.Add("https://localhost:5009");
                //webApplication.Run();
            }
            #endregion

            #region web主机使用 - 控制台命令指定端口
            {
                ////dotnet run --urls="http://localhost:5000"
                ////WebApplicationBuilder
                //WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder();
                ////创建泛型主机
                ////webApplicationBuilder.Services.AddControllers();
                ////webApplicationBuilder.Services.AddMvc();
                //WebApplication webApplication = webApplicationBuilder.Build();

                //webApplication.Run();
            }
            #endregion

            #region web主机使用 - 内置托管主机
            {
                ////dotnet run --urls="http://localhost:5000"
                ////WebApplicationBuilder
                //WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder();
                ////使用内置Kestrel托管主机
                //webApplicationBuilder.WebHost.UseIIS();
                ////创建泛型主机
                //WebApplication webApplication = webApplicationBuilder.Build();

                //webApplication.Run();
            }
            #endregion

            #region web主机使用 - 外网访问  http://+:5010
            {
                ////dotnet run --urls="http://localhost:5000"
                ////WebApplicationBuilder
                //WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder();
                ////使用内置Kestrel托管主机
                //webApplicationBuilder.WebHost.UseKestrel();
                ////创建泛型主机
                //WebApplication webApplication = webApplicationBuilder.Build();
                //webApplication.Urls.Add("http://+:5010");
                //webApplication.Run();
            }
            #endregion

            #region web主机使用 - 中间件
            {
                ////dotnet run --urls="http://localhost:5000"
                ////WebApplicationBuilder
                //WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder();
                ////使用内置Kestrel托管主机
                //webApplicationBuilder.WebHost.UseKestrel();
                ////创建web主机
                //WebApplication webApplication = webApplicationBuilder.Build();
                //var dictiony = Directory.GetCurrentDirectory();
                //webApplication.Urls.Add("http://+:5010");
                ////webApplication.UseStaticFiles();
                //webApplication.Run(async (x) =>
                //{
                //    string path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "htmlpage.html");
                //    //string text = await File.ReadAllTextAsync(path);
                //    //x.Response.ContentType = "text/plain; charset=utf-8";
                //    //await x.Response.WriteAsync(text);
                //    //await x.Response.WriteAsync("我");

                //    //var strem = new FileStream(path, FileMode.Open, FileAccess.Read);
                //    //x.Response.ContentType = "application/octet-stream";
                //    //await strem.CopyToAsync(x.Response.Body);
                //    //strem.Close();
                //    //await x.Response.CompleteAsync();

                //    x.Response.ContentType = "application/json;charset=utf-8";
                //    await x.Response.WriteAsJsonAsync(new User { Id = 1, Name = "握手" });
                //});
                //webApplication.Run();
            }
            #endregion

            #region web主机使用 - 中间件链
            {
                //dotnet run --urls="http://localhost:5000"
                //WebApplicationBuilder
                WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder();
                // 内置托管主机切换。默认:Kestrel IIS HttpSys
                webApplicationBuilder.WebHost.UseKestrel();
                //创建web主机
                WebApplication webApplication = webApplicationBuilder.Build();
                var dictiony = Directory.GetCurrentDirectory();
                webApplication.Urls.Add("http://+:5010");
                //配置中间件链
                // RequestDelegate 创建当前的中间件 RequestDelegate 下一个中间件
                // 1、响应Html中间件
                webApplication.Use(next =>
                {
                    return async x =>
                    {
                        var path = x.Request.Path.Value;
                        if (path.Equals("/html"))
                        {
                            await ResposeHtml(x);
                        }
                        else
                        {
                            // 2、给下一个中间件
                            await next(x);
                        }
                    };
                });

                // 2、响应css中间件
                webApplication.Use(async (context, next) =>
                {
                    // 1、获取请求路径 /js  /html /css /
                    var path = context.Request.Path.Value;
                    if (path.Equals("/css"))
                    {
                        await ResposeCss(context);
                    }
                    else
                    {
                        // 2、给下一个中间件
                        await next();
                    }

                });

                // 3、响应js中间件
                webApplication.Use(async (context, next) =>
                {
                    // 1、获取请求路径 /js  /html /css /
                    var path = context.Request.Path.Value;
                    if (path.Equals("/js"))
                    {
                        await ResposeJs(context);
                    }
                    else
                    {
                        // 2、给下一个中间件
                        await next();
                    }
                });
                // 3、运行项目
                webApplication.Run();
            }
            #endregion
             
            #region web主机使用 - 中间件链 封装中间件
            {
                ////dotnet run --urls="http://localhost:5000"
                ////WebApplicationBuilder
                //WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder();
                //// 内置托管主机切换。默认:Kestrel IIS HttpSys
                //webApplicationBuilder.WebHost.UseKestrel();
                ////创建web主机
                //WebApplication webApplication = webApplicationBuilder.Build();
                //var dictiony = Directory.GetCurrentDirectory();
                //webApplication.Urls.Add("http://+:5010");
                ////配置中间件链
                //// RequestDelegate 创建当前的中间件 RequestDelegate 下一个中间件
                //webApplication.UseCss();
                //webApplication.UseHtml();
                //webApplication.UseJs();

                //// 3、运行项目
                //webApplication.Run();
            }
            #endregion

            #region web主机使用 - 中间件链 封装中间件
            {
                //dotnet run --urls="http://localhost:5000"
                //WebApplicationBuilder
                WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder();
                // 内置托管主机切换。默认:Kestrel IIS HttpSys
                webApplicationBuilder.WebHost.UseKestrel();
                //创建web主机
                WebApplication webApplication = webApplicationBuilder.Build();
                var dictiony = Directory.GetCurrentDirectory();
                webApplication.Urls.Add("http://+:5010");
                //配置中间件链
                // RequestDelegate 创建当前的中间件 RequestDelegate 下一个中间件
                webApplication.UseException();
                webApplication.UseCss();
                webApplication.UseHtml();
                webApplication.UseJs();

                // 3、运行项目
                webApplication.Run();
            }
            #endregion
        }
    }
}
